# -*- coding: utf-8 -*-
import csv
from py2neo import Graph, Node, Relationship, NodeMatcher
from config.database import DatabaseConfig

# 从配置文件获取数据库配置
db_config = DatabaseConfig.get_neo4j_config()
g = Graph(db_config["uri"], auth=db_config["auth"])

# 创建实体
def create_entities():
    # 导入专业实体
    with open('resource/data/课程知识图谱数据/实体节点/专业.csv', 'r', encoding='ANSI') as f:
        reader = csv.reader(f)
        for item in reader:
            if reader.line_num == 1:
                continue
            print("当前行数：", reader.line_num, "当前内容", item)
            test_node_1 = Node("专业", id=item[0], name=item[1])
            g.merge(test_node_1, "专业", "id")
    
    # ... 其他实体导入代码 ...

# 创建关系
def create_relationships():
    matcher = NodeMatcher(g)
    
    # 导入课程-课程基本信息关系
    with open('resource/data/课程知识图谱数据/关系/课程-包含-课程基本信息.csv', 'r', encoding='ANSI') as f:
        reader = csv.reader(f)
        for item in reader:
            if reader.line_num == 1:
                continue
            print("当前关系：", reader.line_num, "当前内容", item)
            findnode = matcher.match('课程', id=item[0]).first()
            endnode = matcher.match('课程基本信息', id=item[1]).first()
            relationships = Relationship(findnode, '课程基本信息', endnode)
            g.merge(relationships, "", "id")
    
    # ... 其他关系导入代码 ...

if __name__ == '__main__':
    create_entities()
    create_relationships() 